package guiCode;

import gui.AddFeedbackGUI;
import gui.TeacherMainMenuGUI;
import gui.uploadManager;

import java.sql.Date;
import java.sql.SQLException;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

import secretary.defineSemester;
import teacher.*;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

import flexibility.CachedRowSetTableModel;
import flexibility.uploadFileInfo;
import flexibility.userData;

import com.sun.rowset.CachedRowSetImpl;

public class AddFeedbackcode {
	private AddFeedbackGUI afg;
	public userData T_Data = new userData();
	public TeacherMainMenuGUI tmm ;
	public ClientConsole teacherConnection=TeacherMainMenuCode.getTeacherConnection();
	public CachedRowSetImpl rs;
	public CachedRowSetImpl rdate;
	public CachedRowSetImpl rdate1;
	int myID=TeacherMainMenuCode.getMyID();
	private CachedRowSetTableModel data;
	public uploadManager uploadMngr;
	public uploadFileInfo fileInfo;
	
//	public defineSemester semester=new defineSemester();
	//int currentSem;
	
	public CachedRowSetImpl addAss(String courseID){
		getcurrassignment gca=new getcurrassignment();
		rs=gca.getcurrassignments(teacherConnection, rs, T_Data, myID, courseID);
		return rs;
	}
	public void runUploadManager(String sid,String aID,int f){
		Integer asid=null;//student id need to be Double
		Integer aassid=null;//ass id
		//f=1 if is evaluation file and 2 if is comments file 
		getSidASSid gcID = new getSidASSid();
		getFeedbackFiles gff=new getFeedbackFiles();
		System.out.println(sid);
		System.out.println(aID);
		rs=gcID.getSidASSid(teacherConnection, rs, T_Data, myID,sid,aID);
		
		
		try {
			if(rs.next())
			{
				System.out.println("get to");
				asid =rs.getInt(1);
				aassid=rs.getInt(2);
				
				
			}
			else
			{
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rs=gff.getFeedbackFile(teacherConnection, rs, T_Data, myID, sid, aID, f);
		fileInfo = new uploadFileInfo();
		fileInfo.setIfUpdate(false);
		try {
			if(rs.next()){
				Integer fileid;
				fileid=rs.getInt(1);
				System.out.println(fileid);
				if(fileid!=0){
					System.out.println("file dd");
				fileInfo.setIfUpdate(true);
				fileInfo.setfileID(fileid);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		fileInfo.setRole(3);
		fileInfo.setTeacherID(myID);
		
		fileInfo.setAssignmentID(aassid);
		fileInfo.setStudentID(asid);
		fileInfo.setTeacherFileType(f);//f=1 Uploading evaluation file, f=2 Uploading comments file 
		uploadMngr = new uploadManager(teacherConnection, myID, fileInfo);
	}
	
	
	
	public void update(AddFeedbackGUI afg,CachedRowSetImpl rs){
	JTableHeader tableHeader=new JTableHeader();
		
		
		try {
			afg.table.setModel(data = new CachedRowSetTableModel(rs));
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer.setHorizontalAlignment(JLabel.CENTER);
			for (int i = 0; i < afg.table.getColumnCount(); i++)
				afg.table.getColumnModel().getColumn(i)
						.setCellRenderer(centerRenderer);
			afg.table.setEnabled(true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
	
	public CachedRowSetImpl addStudents(String ASSID){
		
		getStudent gs=new getStudent();
		rs=gs.getStudent(teacherConnection, rs, T_Data, myID, ASSID);
		return rs;
	}
	public void updatetablestudent(AddFeedbackGUI afg,CachedRowSetImpl rs1){
		//Date CommDate =null;  
		//Date assDate = null;
		JTableHeader tableHeader=new JTableHeader();
           /* getAssDate gd=new getAssDate();
            rdate=gd.getAssDate(teacherConnection, rdate, T_Data, myID, ASSDate);
            try {
    			if(rdate.next()){
    				assDate=rdate.getDate(1);	
    			}
    			else{
    				System.out.println("not");
    			}
    			
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}*/
		try {
			afg.table_1.setModel(data = new CachedRowSetTableModel(rs1));
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer.setHorizontalAlignment(JLabel.CENTER);
			for (int i = 0; i < afg.table_1.getColumnCount(); i++)
				afg.table_1.getColumnModel().getColumn(i)
						.setCellRenderer(centerRenderer);
			afg.table_1.setEnabled(true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*int rowCount=afg.table_1.getRowCount();
		for(int i=0;i<rowCount;i++){
			String dcomit=afg.table_1.getValueAt(i,3).toString();
			getCommitDate gcd=new getCommitDate();
            rdate1=gcd.getCommitDate(teacherConnection, rdate1, T_Data, myID, dcomit);
            try {
            	if(rdate1.next()){
				CommDate=rdate1.getDate(1);
				
            	}
            	else{
            		
            	}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            
            try {
				System.out.println(rdate.getString(1));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            try {
				System.out.println(rdate1.getString(1));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            
           if(CommDate.before(assDate)){
        	   System.out.println("fff");
        	  // afg.table_1.setValueAt("ok",0,0);
        	   //afg.table_1.getModel().setValueAt("OK",0,0);
        	   System.out.println(i);
           }
           else{
        	   afg.table_1.setValueAt("No", i, 4);
           }
			
		}*/
		
		afg.table_1.setVisible(true);
	}
	
}
